// packagePay/pages/project-bill/project-bill.js
import {
    circumstancePayment,
    getPaid,
    staffReckoning,
    stopPay,
    getBillList,
    billStatistics,
    getxmzdlistapi
} from '../../../packagePay/api/pay'
import user from '../../../utils/user.js'
import {
    dateFormat,
    shake
} from '../../../utils/util'
const app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        arr: [{
                name: '项目账单',
                index: 0
            },
            {
                name: '员工账单',
                index: 1
            },
            {
                name: '账单明细',
                index: 2
            }
        ],
        tabs: 0,
        title: '筛选',
        cancel: '1',
        screen: false,
        select: 0,
        selectId: '',
        table: [{
            name: '全部',
            index: 0,
            id: ''
        }, {
            name: '已领薪',
            index: 1,
            id: 1
        }, {
            name: '待领薪',
            index: 2,
            id: 0
        }, {
            name: '停止发薪',
            index: 3,
            id: 2
        }, {
            name: '领薪失败',
            index: 4,
            id: 4
        }, {
            name: '进行中',
            index: 5,
            id: 3
        }],
        seekItemText: '',
        seekStaffText: '',
        personnelList: [],
        personnelNextPage: false,
        servantPageNum: 1,
        itemPageNum: 1,
        itemHasNextPage: false,
        isAdmin: false,
        data: [],
        waitMoney: Number,
        alrMoney: Number,
        allMoney: Number,
        personnelTotalCount: Number,
        personnelTotalMoney: Number,
        projectMonth: '',
        personnerMonth: '',
        onItemDisplay: true,
        onSeekStaffValue: '',
        onStaffDisplay: true,
        currentTime:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options, 'options')
        user.waitLogin().then(() => {
          this.setData({
              data: [],
              itemPageNum: 1,
              projectMonth: dateFormat('YYYY-mm', new Date()),
              currentTime:dateFormat('YYYY-mm-dd HH:MM:SS.ss', new Date())
          }, () => {
            //   this.getBillList()
              this.getBillStatistics()
              this.getxmzdlist()
          })
          console.log(this.data.currentTime, 'currentTime')
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
    //   if(this.data.projectMonth){
    //     this.setData({
    //       isAdmin: !!user.isAdmin,
    //       data: [],
    //       itemPageNum: 1,
    //       projectMonth: dateFormat('YYYY-mm', new Date()),
    //     }, () => {
    //         this.getBillList()
    //     })
    //   }
    },
    //(this.data.select ==0? null :this.data.select)
    // currentTime:this.data.currentTime
    staffReckoning(search = false) {
        staffReckoning({
            month: this.data.personnerMonth,
            status: this.data.selectId === '' ? '' : this.data.selectId,
            searchUserInfo: this.data.seekStaffText,
            pageSize: 50,
            pageNum: this.data.servantPageNum,
        }).then(res => {
          console.log(res,'发薪人数')
            if (!res.hasNextPage) {
                this.data.personnelNextPage = false
            } else {
                this.data.personnelNextPage = true
            }
            res.list.map((item, index) => {
                res.list[index].idCardNo = item.idCardNo ? item.idCardNo.substr(14, 4) : '暂无'
            })
            let list = []
            list.push(...this.data.personnelList)
            list.push(...res.list)
            console.log(res.list, 'res.list')
            console.log(list, 'listlist')
            this.setData({
                personnelList: list,
                personnelNextPage: res.hasNextPage,
                personnelTotalCount: res.count,
                personnelTotalMoney: res.money,
                // pageNum:pageNum
            })
        })
    },
    async getBillStatistics(){
        const result = await billStatistics({
            month: this.data.projectMonth
        })
        console.log(result, 'result166')
        this.setData({
            waitMoney: result.waitMoney,
            alrMoney: result.alrMoney,
            allMoney: result.allMoney,
        })
    },
    getxmzdlist(){
        getxmzdlistapi({
            month: this.data.projectMonth,
            pageSize: 50,
            pageNum: this.data.itemPageNum,
            projectName: this.data.seekItemText,
            queryTime:this.data.currentTime
        }).then((res)=>{
            if (!res.hasNextPage) {
                this.data.itemHasNextPage = false
            } else {
                this.data.itemHasNextPage = true
            }
            let arr = []
            arr.push(...this.data.data)
            arr.push(...res.list)
            this.setData({
                data: arr,
                itemHasNextPage: res.hasNextPage
            })
        })
    },
    getBillList() {
        console.log(this.data.currentTime, 'currentTime')
        getBillList({
            month: this.data.projectMonth,
            pageSize: 50,
            pageNum: this.data.itemPageNum,
            projectName: this.data.seekItemText,
            queryTime:this.data.currentTime
        }).then(res => {
            console.log(res, 'getBillListgetBillList')
            if (!res.hasNextPage) {
                this.data.itemHasNextPage = false
            } else {
                this.data.itemHasNextPage = true
            }
            let arr = []
            arr.push(...this.data.data)
            arr.push(...res.list)
            // waitMoney: res.waitMoney,
            // alrMoney: res.alrMoney,
            // allMoney: res.allMoney,
            this.setData({
                data: arr,
                itemHasNextPage: res.hasNextPage
            })
        })
    },
    onStop(event) {
        console.log(event, 'eventevent')
        app.confirm('是否确认停止该员工发薪').then(() => {
            stopPay({
                id: event.currentTarget.dataset.id
            }).then(() => {
                app.toast('停止发薪成功', 'success')
                this.setData({
                    servantPageNum: 1,
                    personnelList: []
                }, () => {
                    this.staffReckoning()
                })
            })
        })
    },
    onSkip(event) {
        let id = event.currentTarget.dataset.id
        wx.navigateTo({
            url: '/packagePay/pages/bill-detail/bill-detail?id=' + id,
            events: {
                // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
                monitorChange:(data) => {
                    console.log(data.data, '返回值')
                    if(!data.data){
                        return
                    }else{
                        this.setData({
                            isAdmin: !!user.isAdmin,
                            data: [],
                            itemPageNum: 1,
                            projectMonth: this.data.projectMonth || dateFormat('YYYY-mm', new Date()),
                        }, () => {
                            this.getBillList()
                        })
                    }
                }
            },
        })
    },
    onJump(event) {
        console.log(event, 'onJumponJump')
        if (event.currentTarget.dataset.status !== 0) {
            let id = event.currentTarget.dataset.id
            wx.navigateTo({
                url: '/packagePay/pages/individual-detail/individual-detail?id=' + id,
            })
        }
    },
    onCall(event) {
        console.log(event, '电话电话电话')
        wx.makePhoneCall({
            phoneNumber: event.currentTarget.dataset.phone
        })
    },
    onChangeSelect(event) {
        console.log(event, '呵呵呵呵呵')
        this.setData({
            select: event.currentTarget.dataset.index,
            selectId: event.currentTarget.dataset.id,
            screen: false,
            personnelList: [],
            servantPageNum: 1,

        })
        this.staffReckoning()
    },
    onClose(event) {
        // console.log(event, '子组件回复的内容')
        this.setData({
            screen: false
        })
    },
    onScreen(event) {
        // console.log(event, '哈哈哈哈哈')
        this.setData({
            screen: true
        })
    },
    onChange(event) {
        // console.log(event, '哈哈哈哈哈哈哈')
        this.setData({
            tabs: event.currentTarget.dataset.index
        }, () => {
            if (this.data.tabs == 1) {
                this.setData({
                    personnelList: [],
                    servantPageNum: 1,
                    personnerMonth: dateFormat('YYYY-mm', new Date()),
                    data: [],
                    itemPageNum: 1,
                    projectMonth: dateFormat('YYYY-mm', new Date()),
                    seekItemText:'',
                    onItemDisplay:true
                },()=>{
                    this.staffReckoning()
                })
            }else{
                console.log(this.isAdmin)
                this.setData({
                    data: [],
                    itemPageNum: 1,
                    projectMonth: dateFormat('YYYY-mm', new Date()),
                    personnelList: [],
                    servantPageNum: 1,
                    personnerMonth: dateFormat('YYYY-mm', new Date()),
                    seekStaffText:'',
                    onStaffDisplay:true,
                    select:0,
                    selectId:'',
                    currentTime: dateFormat('YYYY-mm-dd HH:MM:SS.ss', new Date())
                }, () => {
                    this.getBillList()
                    this.getBillStatistics()
                })
            }
        })
        
    },
    scrollTolower(event) {
        console.log(event, '滚动到了底部')
        console.log(this.data.personnelNextPage, '')
        if (this.data.tabs == 1) {
            if (this.data.personnelNextPage) {
                let servantPageNum = this.data.servantPageNum
                servantPageNum += 1
                this.setData({
                    // personnelList:[],
                    servantPageNum: servantPageNum
                }, () => {
                    this.staffReckoning()
                })
            }else{
                return
            }
        } else {
            if (this.data.itemHasNextPage) {
                let itemPageNum = this.data.itemPageNum
                itemPageNum += 1
                this.setData({
                    itemPageNum: itemPageNum
                }, () => {
                    this.getBillList()
                })
            } else {
                return
            }
        }
    },
    onProjectMonthChange(event) {
        this.setData({
            projectMonth: event.detail.value,
            data: [],
            itemPageNum: 1,
            currentTime:dateFormat('YYYY-mm-dd HH:MM:SS.ss', new Date())
        }, () => {
            this.getBillList()
            this.getBillStatistics()
        })
    },
    onPersonnelMonthChange(event) {
        console.log(event, '月份............................')
        this.setData({
            personnerMonth: event.detail.value,
            personnelList: [],
            servantPageNum: 1,
        }, () => {
            this.staffReckoning()
        })
    },
    onSeekItemInput(event) {
        console.log(event, '搜索inputinput')
        // if(this.data.tabs == 0){
        shake(() => {
            if (event.detail.value) {
                this.setData({
                    seekItemText: event.detail.value,
                    data: [],
                    itemPageNum: 1,
                    projectMonth: '',
                    onItemDisplay: false
                }, () => {
                    console.log(this.data.seekText, 'this.data.seekTextthis.data.seekText')
                    this.getBillList()
                })
            } else {
                this.setData({
                    seekItemText: event.detail.value,
                    projectMonth: dateFormat('YYYY-mm', new Date()),
                    data: [],
                    itemPageNum: 1,
                    onItemDisplay: true
                }, () => {
                    console.log(this.data.seekText, 'this.data.seekTextthis.data.seekText')
                    this.getBillList()
                })
            }
        }, 800)
        // }
    },
    onSeekStaffInput(event) {
        shake(() => {
            if (event.detail.value) {
                this.setData({
                    seekStaffText: event.detail.value,
                    personnelList: [],
                    servantPageNum: 1,
                    personnerMonth: '',
                    onSeekStaffValue: event.detail.value,
                    onStaffDisplay: false
                }, () => {
                    console.log(this.data.seekText, 'this.data.seekTextthis.data.seekText')
                    this.staffReckoning()
                })
            } else {
                this.setData({
                    seekStaffText: event.detail.value,
                    personnerMonth: dateFormat('YYYY-mm', new Date()),
                    personnelList: [],
                    servantPageNum: 1,
                    onStaffDisplay: true
                }, () => {
                    console.log(this.data.seekText, 'this.data.seekTextthis.data.seekText')
                    this.staffReckoning()
                })
            }
        }, 800)
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})